package com.cityfood.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.common.pojo.CommentDisplay;
import com.cityfood.common.utils.JsonUtils;
import com.cityfood.pojo.CfComment;
import com.cityfood.pojo.CfOrder;
import com.cityfood.pojo.CfProduct;
import com.cityfood.pojo.CfUser;
import com.cityfood.service.CommentService;
import com.cityfood.service.OrderService;
import com.cityfood.service.ProductService;
import com.cityfood.service.UserService;
import com.github.pagehelper.PageInfo;

@Controller
public class CommentController {
	
	//商品评论实现类
	@Autowired
	private CommentService commentService;
	
	//商品接口
	@Autowired
	private ProductService productService;
		
	//用户接口
	@Autowired
	private UserService userService;
	
	//用户接口
	@Autowired
	private OrderService orderService;
	
	
	
	//添加商品评论
	@RequestMapping(value="/comment",method=RequestMethod.POST)
	@ResponseBody
	public CityFoodResult insertComment(@RequestParam Integer userId,
										@RequestParam Integer orderId,
										@RequestParam String commentList,
										@RequestParam Integer deliveryStarNum){
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		List<Map> commList = JsonUtils.jsonToList(commentList, Map.class);
		for (Map<String,Object> comm : commList) {
			
			CfComment comment=new CfComment();
			comment.setUserId(userId);
			comment.setProductId(Integer.parseInt(comm.get("productId").toString()));
			comment.setCommentNumber(Byte.parseByte(comm.get("starNumber").toString()));
			if (!comm.containsKey("content")) {
				comment.setContent("默认评论");
			}else{
				comment.setContent(comm.get("content").toString());
			}
			
			comment.setCommentImage(comm.get("images").toString());
			comment.setCreateTime(new Date());
			
			int i = commentService.insertComment(comment);
			if (i == 0) {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("添加商品评论失败");
				cityFoodResult.setData(false);
				return cityFoodResult;
			}
		}
		CfOrder order=new CfOrder();
		order.setId(orderId);
		order.setTradeStatus((byte)3);
		//修改订单状态为已完成
		int i = orderService.updateOrderStatus(order);
		if (i<=0) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("订单评论失败");
			cityFoodResult.setData(false);
			return cityFoodResult;
		}
		cityFoodResult.setCode(200);
		cityFoodResult.setMsg("添加商品评论成功");
		cityFoodResult.setData(true);
		return cityFoodResult;
		
		
	}
	
	
	
	//后台管理--------------------------------------------------------------------------------------------
	
	// 根据商品id获取商品评论
	@RequestMapping(value="/manage/comment",method=RequestMethod.GET)
		@ResponseBody
		public CityFoodResult getCommentList(@RequestParam(defaultValue="") String commentMap,
				   							 @RequestParam(defaultValue="1")Integer currentPage){
			CityFoodResult cityFoodResult = new CityFoodResult();
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			
			//商品评论展示包装类集合
			List<Map<String,Object>> ls = new ArrayList<>();

			Map<String,Object> cMap = new LinkedHashMap<>();
			if (!commentMap.equals("")) {
				cMap = (Map<String,Object>)JsonUtils.jsonToMap(commentMap);
			}
			//查询条件
			Map<String,Object> commMap = new LinkedHashMap<String, Object>();
				if (cMap.containsKey("productName")) {
					List<CfProduct> likeName = productService.getProductLikeName((String)cMap.get("productName"));
					//productIds商品id集合
					List<Integer> productIds = new ArrayList<>();
					for (CfProduct cfProduct : likeName) {
						productIds.add(cfProduct.getId());
					}
					
					if (cMap.containsKey("nickName")) {
						List<CfUser> nickName = userService.getUserLikeNickName((String)cMap.get("nickName"));
						//userIds用户id集合
						List<Integer> userIds = new ArrayList<>();
						for (CfUser user : nickName) {
							userIds.add(user.getId());
						}
						
						if (cMap.containsKey("starNumber")) {
							//商品名称模糊查询
							commMap.put("productIds", productIds);
							//用户名称模糊查询
							commMap.put("userIds", userIds);
							//评论星级
							commMap.put("starNumber", cMap.get("starNumber"));
						}else{
							
						//商品名称模糊查询
						commMap.put("productIds", productIds);
						//用户名称模糊查询
						commMap.put("userIds", userIds);
						}
					}else{
						if (cMap.containsKey("starNumber")) {
							//商品名称模糊查询
							commMap.put("productIds", productIds);
							//评论星级
							commMap.put("starNumber", cMap.get("starNumber"));
						}else{
						//只有商品名称模糊查询
						commMap.put("productIds", productIds);
						}
					}
					
				}else{
					//没有商品名称的情况
					if (cMap.containsKey("nickName")) {
						List<CfUser> nickName = userService.getUserLikeNickName((String)cMap.get("nickName"));
						//userIds用户id集合
						List<Integer> userIds = new ArrayList<>();
						for (CfUser user : nickName) {
							userIds.add(user.getId());
						}
						
						if (cMap.containsKey("starNumber")) {
							//用户名称模糊查询
							commMap.put("userIds", userIds);
							//评论星级
							commMap.put("starNumber", cMap.get("starNumber"));
						}else{
							
						//用户名称模糊查询
						commMap.put("userIds", userIds);
						}
					}else{
						//没有用户名称
						if (cMap.containsKey("starNumber")) {
							//评论星级
							commMap.put("starNumber", cMap.get("starNumber"));
						}
					}
				}
				
				PageInfo<CfComment> list = commentService.getCommentByCommentMapAndPage(commMap, currentPage, 20);
				if (list!=null) {
					if (list.getList().size()==0) {
						cityFoodResult.setCode(201);
						cityFoodResult.setMsg("暂无商品评价哦~");
						cityFoodResult.setData("{}");
						return cityFoodResult;
					}
					for (CfComment comment : list.getList()) {
						CfProduct product = productService.getProductByProductId(comment.getProductId());
							//商品评论展示包装类
							Map<String,Object> ms = new LinkedHashMap<>();
							CfUser user = userService.getUserByUserId(comment.getUserId());
							ms.put("id", comment.getId());
							ms.put("userPic", user.getAvatarurl());
							ms.put("nickName", user.getNickname());
							ms.put("commentNum", comment.getCommentNumber());
							ms.put("content", comment.getContent());
							ms.put("contentPic", comment.getCommentImage());
							ms.put("commentDate", sd.format(comment.getCreateTime()));
							ls.add(ms);
					}
					
			
			
				Map<String,Object> mp = new LinkedHashMap<>();

				//存入查询到的总数
				mp.put("total", list.getTotal());

				//存入需要展示的数据
				mp.put("comments", ls);
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("查询商品评论成功");
				cityFoodResult.setData(mp);
				return cityFoodResult;
			}
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("暂无商品评价哦~");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}

		
		
	// 后台管理---------------------------------------------------------------------------

	// 根据商品评价信息删除商品评价,delete
	@RequestMapping(value = "/manage/comment/remove", method = RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteComment(@RequestBody Map map){
	    String id = map.get("id").toString();
		CityFoodResult cityFoodResult = new CityFoodResult();
		CfComment comment=new CfComment();
		comment.setId(Integer.parseInt(id));
		int i = commentService.deleteComment(comment);
		if (i > 0) {
			cityFoodResult.setCode(200);
			cityFoodResult.setMsg("删除商品评价成功");
			cityFoodResult.setData(true);
		} else {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("删除商品评价失败");
			cityFoodResult.setData(false);
		}
		return cityFoodResult;
	}
	
	@RequestMapping(value = "/manage/comment/batchremove", method = RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteComments(@RequestBody Map map){
	    String ids = map.get("ids").toString();
		CityFoodResult cityFoodResult = new CityFoodResult();
		
		String[] strIds = ids.split(",");
		for (String strId : strIds) {
			CfComment comment=new CfComment();
			comment.setId(Integer.parseInt(strId));
			int i = commentService.deleteComment(comment);
			if (i > 0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("删除商品评价成功");
				cityFoodResult.setData(true);
			} else {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("删除商品评价失败");
				cityFoodResult.setData(false);
				return cityFoodResult;
			}
		}
		return cityFoodResult;
	}

}
